

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.xml.stream.XMLStreamException;

import at.ac.tuwien.infosys.dsg.daios.exceptions.AmbigiousException;
import at.ac.tuwien.infosys.dsg.daios.exceptions.InvocationException;
import at.ac.tuwien.infosys.dsg.daios.framework.DefaultInterceptor;
import at.ac.tuwien.infosys.dsg.daios.framework.ServiceFrontend;
import at.ac.tuwien.infosys.dsg.daios.framework.ServiceFrontendFactory;
import at.ac.tuwien.infosys.dsg.daios.framework.interfaces.IServiceFrontendImplementor;
import at.ac.tuwien.infosys.dsg.daios.framework.structs.DaiosInputMessage;
import at.ac.tuwien.infosys.dsg.daios.framework.structs.DaiosOutputMessage;
import at.ac.tuwien.infosys.dsg.daios.wsdl.WSDLEncoding;
import at.ac.tuwien.infosys.dsg.daios.wsdl.interfaces.IWSDL;
import at.ac.tuwien.infosys.dsg.daios.wsdl.interfaces.IWSDLMessage;
import at.ac.tuwien.infosys.dsg.daios.wsdl.interfaces.IWSDLOperation;
import at.ac.tuwien.infosys.dsg.daios.wsdl.typesystem.WSDLTypeTree;

public class DaiosRuntimeAnalysis {

	public static void main(String[] args) throws MalformedURLException,
		WSDLException, AmbigiousException, XMLStreamException, IOException,
		InvocationException {
		
		String protocol = "rest";
		String style = "doc";
		String backend = "native";
		int invs = Integer.parseInt("1");
		
		new DaiosRuntimeAnalysis().run(protocol, style, backend, invs);
		
	}
	
	private static String load(String path_big_string)
		throws IOException {
		
		BufferedReader reader = new BufferedReader(
			new FileReader(path_big_string));
		
		StringBuffer sb = new StringBuffer();
		
		String s = null;
		while((s = reader.readLine()) != null)
			sb.append(s);
		
		return sb.toString();
		
	}
	
	private void run(String protocol, String style, String backend, int invs)
		throws MalformedURLException, WSDLException,
		AmbigiousException, XMLStreamException, IOException,
		InvocationException {
		
		ServiceFrontendFactory fac = null;
		if(backend.equals("native"))
			fac = ServiceFrontendFactory.getFactory
				("at.ac.tuwien.infosys.dsg.daiosPlugins.nativeInvoker.NativeServiceInvokerFactory");
		else
			fac = ServiceFrontendFactory.getFactory
			("at.ac.tuwien.infosys.dsg.daiosPlugins.axis2.Axis2ServiceInvokerFactory");
		
		ServiceFrontend frontend = null;
		if(protocol.equals("soap") && style.equals("doc"))
			frontend = fac.createFrontend(
				new URL("http://localhost:8080/axis2/services/DADocTestService?wsdl"),
				null);
		else if(protocol.equals("soap") && style.equals("rpc"))
			frontend = fac.createFrontend(
				new URL("??????"),
				null);
		else {
			frontend = fac.createFrontend(
					new URL("file:///home/philipp/workspace/Daios/examples/getString.xml"),
					null);
			frontend.setEndpointAddress(new URL(
					"http://localhost:8080/axis2/rest/DADocTestService"));
		}
		
		// do a 'dummy invocation' first
		frontend.setInterceptor(null);
		DaiosInputMessage in = new DaiosInputMessage();
		in.setString("param0", "Leitner");
		frontend.requestResponse(in);
		frontend.setInterceptor(null);
		
		for(int i=0; i<invs; i++) {
		
			in = new DaiosInputMessage();
			in.setString("param0", "Leitner");
//			in.setString("param0", load("bigstring.txt"));
			frontend.requestResponse(in);
			
		}
		
	}
	
}
